package cn.com.nthotline.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import cn.com.nthotline.R;
import cn.com.nthotline.activity.ArbitraryFragmentActivity;

public class FragmentUtil {
	public static final String ARGUMENTS_KEY_NO_BACK_STACK = "noBackStack";

	public static void navigateToInNewActivity(Context activity,
			Class<? extends Fragment> fragment, Bundle bundle, int layout) {
		Intent intent = new Intent(activity, ArbitraryFragmentActivity.class);
		intent.putExtra(ArbitraryFragmentActivity.EXTRAS_BUNDLE, bundle);
		intent.putExtra(ArbitraryFragmentActivity.EXTRAS_FRAGMENT_CLASS_NAME,
				fragment.getName());
		activity.startActivity(intent);
		// activity.overridePendingTransition(R.anim.push_left_in,
		// R.anim.push_left_out);
	}

	public static void navigateTo(FragmentManager manager, Fragment fragment,
			Bundle bundle) {
		System.out.println("fragment===>>>   " + fragment.getClass().getName());
		FragmentTransaction transaction = manager.beginTransaction();
		if (bundle != null && bundle.getBoolean(ARGUMENTS_KEY_NO_BACK_STACK)) {
			bundle.remove("ARGUMENTS_KEY_NO_BACK_STACK");
		} else {
			transaction.addToBackStack(null);
		}
		fragment.setArguments(bundle);
//		 transaction.setCustomAnimations(R.anim.push_left_in,
//		 R.anim.push_left_out);
//		 transaction.setCustomAnimations(R.anim.slide_in_bottom,
//				 R.anim.slide_out_bottom,R.anim.slide_in_top,R.anim.slide_out_top);
//		transaction.setCustomAnimations(R.anim.push_left_in,
//				R.anim.push_left_out, R.anim.push_right_in,
//				R.anim.push_right_out);// R.anim.push_left_out,R.anim.push_right_out);//,R.anim.push_left_in,
										// R.anim.push_right_out);
		transaction.replace(R.id.layout_container_main, fragment);
		transaction.addToBackStack(null);
//		transaction.commitAllowingStateLoss();
		transaction.commit();
		manager.executePendingTransactions();
	}

	public static void navigateAdd(FragmentManager manager, Fragment fragment,
			Bundle bundle) {
		FragmentTransaction transaction = manager.beginTransaction();
		if (bundle != null && bundle.getBoolean(ARGUMENTS_KEY_NO_BACK_STACK)) {
			bundle.remove("ARGUMENTS_KEY_NO_BACK_STACK");
		} else {
			transaction.addToBackStack(null);
		}
		fragment.setArguments(bundle);
		// transaction.setCustomAnimations(R.anim.push_left_in,
		// R.anim.push_left_out);
		transaction.setCustomAnimations(R.anim.push_left_in,
				R.anim.push_left_out, R.anim.push_right_in,
				R.anim.push_right_out);
		transaction.add(R.id.layout_container_main, fragment);
		// transaction.addToBackStack(null);
		transaction.commit();
		manager.executePendingTransactions();
	}
}
